package net.csdn.business.service;

import com.alibaba.fastjson2.JSONObject;
import io.grpc.stub.StreamObserver;
import lombok.extern.slf4j.Slf4j;
import net.csdn.business.common.utils.JsonUtil;
import net.csdn.business.pojo.*;
import net.devh.boot.grpc.server.service.GrpcService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Slf4j
@GrpcService
public class AtomGitServiceImpl extends AtomGitCSDNRepoServiceGrpc.AtomGitCSDNRepoServiceImplBase {
    @Autowired
    private MigrateSevice migrateSevice;

    @Override
    public void importAtomGitUser(AtomGitUserList request, StreamObserver<AtomGitServiceMessage> responseObserver) {
            AtomGitServiceMessage rep;
        try {
            List<AtomGitUser> userListList = request.getUserListList();
            Integer succesNum = migrateSevice.importAtomGitUser(userListList);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("total",userListList.size());
            jsonObject.put("success",succesNum);
            rep = AtomGitServiceMessage.newBuilder().setSuccess(true).setMsg(jsonObject.toString()).build();
            // 向客户端输出数据
            responseObserver.onNext(rep);
            // 结束输出
            responseObserver.onCompleted();
        } catch (Exception e) {
            log.error("importAtomGitUser error:{},", JsonUtil.toJsonString(e));
            rep = AtomGitServiceMessage.newBuilder().setSuccess(false).setMsg("error").build();
            // 向客户端输出数据
            responseObserver.onNext(rep);
            // 结束输出
            responseObserver.onCompleted();
        }
    }



    @Override
    public void importAtomGitGroupMember(AtomGitGroupMemberList request, StreamObserver<AtomGitServiceMessage> responseObserver) {
            AtomGitServiceMessage rep;
        try {
            List<AtomGitGroupMember> memberList = request.getMemberListList();
            Integer succesNum = migrateSevice.importAtomGitGroupMember(memberList);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("total",memberList.size());
            jsonObject.put("success",succesNum);
            rep = AtomGitServiceMessage.newBuilder().setSuccess(true).setMsg(jsonObject.toString()).build();
            // 向客户端输出数据
            responseObserver.onNext(rep);
            // 结束输出
            responseObserver.onCompleted();
        } catch (Exception e) {
            log.error("importAtomGitGroupMember error:{}", JsonUtil.toJsonString(e));
            rep = AtomGitServiceMessage.newBuilder().setSuccess(false).setMsg("error").build();
            // 向客户端输出数据
            responseObserver.onNext(rep);
            // 结束输出
            responseObserver.onCompleted();
        }
    }

    @Override
    public void importAtomGitIssue(AtomGitIssueList request, StreamObserver<AtomGitServiceMessage> responseObserver) {
            Long repoId = request.getRepoId();
            String repoName = request.getRepoName();
            String repoGroupName = request.getRepoGroupName();
            List<AtomGitIssue> issueListList = request.getIssueListList();
            AtomGitServiceMessage rep;
        try {

            Integer succesNum = migrateSevice.importAtomGitIssue(repoId,repoGroupName, repoName, issueListList);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("total",issueListList.size());
            jsonObject.put("success",succesNum);
            rep = AtomGitServiceMessage.newBuilder().setSuccess(true).setMsg(jsonObject.toString()).build();
            // 向客户端输出数据
            responseObserver.onNext(rep);
            // 结束输出
            responseObserver.onCompleted();
        } catch (Exception e) {
            log.error("importAtomGitIssue repoId:{},repoName:{},repoGroupName:{}", repoId,repoName,repoGroupName);
            log.error("importAtomGitIssue error:{},", JsonUtil.toJsonString(e));
            rep = AtomGitServiceMessage.newBuilder().setSuccess(false).setMsg("error").build();
            // 向客户端输出数据
            responseObserver.onNext(rep);
            // 结束输出
            responseObserver.onCompleted();
        }
    }

    //导入mileStone
    @Override
    public void importAtomGitMilestone(AtomGitMileStoneList request, StreamObserver<AtomGitServiceMessage> responseObserver) {
        //super.importAtomGitMilestone(request, responseObserver);
        Long repoId = request.getRepoId();
        String repoName = request.getRepoName();
        String repoGroupName = request.getRepoGroupName();
        List<AtomGitMileStone> mileStoneList = request.getMileStoneListList();
        AtomGitServiceMessage rep;
        try {
            Integer success = migrateSevice.importMileStone(repoId,repoGroupName,repoName,mileStoneList);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("total",mileStoneList.size());
            jsonObject.put("success",success);
            rep = AtomGitServiceMessage.newBuilder().setSuccess(true).setMsg(jsonObject.toString()).build();
            // 向客户端输出数据
            responseObserver.onNext(rep);
            // 结束输出
            responseObserver.onCompleted();
        } catch (Exception e) {
            log.error("importAtomGitMilestone repoId:{},repoName:{},repoGroupName:{}", repoId,repoName,repoGroupName);
            log.error("importAtomGitMilestone error:{},", JsonUtil.toJsonString(e));
            rep = AtomGitServiceMessage.newBuilder().setSuccess(false).setMsg("error").build();
            // 向客户端输出数据
            responseObserver.onNext(rep);
            // 结束输出
            responseObserver.onCompleted();
        }
    }

    @Override
    public void importAtomGitLabel(AtomGitLabelList request, StreamObserver<AtomGitServiceMessage> responseObserver) {
        //super.importAtomGitLabel(request, responseObserver);
        List<String> groupPathList = request.getGroupPathListList();
        List<AtomGitLabel> labelList = request.getLabelListList();
        log.info("importAtomGitLabel: groups:{},labels:{}",groupPathList.size(),labelList.size());
        AtomGitServiceMessage rep ;
        try {
            Integer succesNum = migrateSevice.importLabels(groupPathList,labelList);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("total",groupPathList.size()*labelList.size());
            jsonObject.put("success",succesNum);
            rep = AtomGitServiceMessage.newBuilder().setSuccess(true).setMsg(jsonObject.toString()).build();
            // 向客户端输出数据
            responseObserver.onNext(rep);
            // 结束输出
            responseObserver.onCompleted();
        } catch (Exception e) {
            log.error("importAtomGitLabel error:{},", JsonUtil.toJsonString(e));
            rep = AtomGitServiceMessage.newBuilder().setSuccess(false).setMsg("error").build();
            // 向客户端输出数据
            responseObserver.onNext(rep);
            // 结束输出
            responseObserver.onCompleted();
        }
    }

    @Override
    public void checkAtomGitUser(AtomGitUserList request, StreamObserver<AtomGitCheckResponse> responseObserver) {
        //super.checkAtomGitUser(request, responseObserver);
        List<Long> ids = migrateSevice.checkAtomGitUser(request);
        AtomGitCheckResponse rep = AtomGitCheckResponse.newBuilder().addAllFailedId(ids).setSuccess(true).build();
        // 向客户端输出数据
        responseObserver.onNext(rep);
        // 结束输出
        responseObserver.onCompleted();
    }

    @Override
    public void checkAtomGitGroupMember(AtomGitGroupMemberList request, StreamObserver<AtomGitCheckGroupMemberResponse> responseObserver) {
        //super.checkAtomGitGroupMember(request, responseObserver);
        List<AtomGitGroupMember> atomGitGroupMembers = migrateSevice.checkAtomGitGroupMember(request);
        AtomGitCheckGroupMemberResponse rep = AtomGitCheckGroupMemberResponse.newBuilder().addAllFailedDataList(atomGitGroupMembers).setSuccess(true).build();
        // 向客户端输出数据
        responseObserver.onNext(rep);
        // 结束输出
        responseObserver.onCompleted();
    }

    @Override
    public void checkAtomGitIssue(AtomGitIssueList request, StreamObserver<AtomGitCheckResponse> responseObserver) {
        //super.checkAtomGitIssue(request, responseObserver);
        List<Long> ids = migrateSevice.checkAtomGitIssue(request);
        AtomGitCheckResponse rep = AtomGitCheckResponse.newBuilder().setSuccess(true).addAllFailedId(ids).build();
        // 向客户端输出数据
        responseObserver.onNext(rep);
        // 结束输出
        responseObserver.onCompleted();
    }

    @Override
    public void checkAtomGitMilestone(AtomGitMileStoneList request, StreamObserver<AtomGitCheckResponse> responseObserver) {
        //super.checkAtomGitMilestone(request, responseObserver);
        List<Long> ids = migrateSevice.checkAtomGitMilestone(request);
        AtomGitCheckResponse rep = AtomGitCheckResponse.newBuilder().addAllFailedId(ids).setSuccess(true).build();
        // 向客户端输出数据
        responseObserver.onNext(rep);
        // 结束输出
        responseObserver.onCompleted();
    }

    @Override
    public void checkAtomGitLabel(AtomGitLabelList request, StreamObserver<AtomGitCheckResponse> responseObserver) {
        //super.checkAtomGitLabel(request, responseObserver);
        List<Long> ids = migrateSevice.checkAtomGitLabel(request);
        AtomGitCheckResponse rep = AtomGitCheckResponse.newBuilder().addAllFailedId(ids).setSuccess(true).build();
        // 向客户端输出数据
        responseObserver.onNext(rep);
        // 结束输出
        responseObserver.onCompleted();
    }
}
